#-*-encoding:utf-8-*-
import optparse
import events
import habits

def gen_conf(day, fname):
    with open(fname, "wb") as f:
        f.write("[global]\n")
        f.write("input=../day_{0}.gps.csv\n".format(day))
        f.write("tmpdir=tmp\n")
        f.write("spillNumber=10\n")
        f.write("prefix=day_{0}\n".format(day))
        f.write("header=True\n")
        f.write("network=../hangzhou.net.xml\n")
        f.write("[risk]\n")
        f.write("overSpeedRatio=1.2\n")
        f.write("overSpeedDuration=10\n")
        f.write("overSpeedOutput=day_{0}.os.feat\n".format(day))
        f.write("abruptAcc=4.0\n")
        f.write("abruptAccOutput=/day_{0}.acc.feat\n".format(day))
        f.write("abruptDec=4.0\n")
        f.write("abruptDecOutput=day_{0}.dec.feat\n".format(day))
        f.write("turnAngle=90\n")
        f.write("turnSpeed=6\n")
        f.write("turnOutput=day_{0}.abruptTurn.feat\n".format(day))

def get_options(args=None) :
    parser = optparse.OptionParser()
    parser.add_option("-s", "--start", dest="start", type="int", help="The start day")
    parser.add_option("-e", "--end", dest="end", type="int", help="end day, e must greater or equal to s")

    (options, args) = parser.parse_args(args=args)
    if not options.start or not options.start:
        parser.print_help()
        exit(0)
    if options.start > options.end:
        parser.print_help()
        exit(0)
    return options

def main(options=None):

    for day in range(options.start, options.end + 1):
        main.network="../hangzhou.net.xml"
        main.gps="../day_{0}.gps.csv".format(day)
        main.output="day_{0}.habit".format(day)
        print "parse day {0} habits".format(day)
        habits.main(main)
        print "parse day {0} habits successfully".format(day)

    for day in range(options.start, options.end + 1):
        fname = "conf_{0}.ini".format(day)
        gen_conf(day, fname)
        # call events
        main.config = fname
        print "begin to recognize events in day {0}".format(day)
        events.process(main)
        print "end to recognize events in day {0}".format(day)


if __name__ == '__main__':
    main(get_options())